import * as XLSX from 'xlsx-js-style';
import dayjs from 'dayjs';
//去重 数组
export function duplicate(arr) {
    if (!Array.isArray(arr)) {
        return;
    }
    var array = [];
    for (var i = 0; i < arr.length; i++) {

        if (array.indexOf(arr[i]) === -1) {

            array.push(arr[i])

        }

    }

    return array;

}
export function exportXlsx(arr, type, name) {
    const wb = XLSX.utils.book_new();
    const ws = XLSX.utils.aoa_to_sheet(arr);

    XLSX.utils.book_append_sheet(wb, ws, "Sheet1");
    ws['!merges'] = [
        { s: { r: 0, c: 0 }, e: { r: 0, c: 5 } },
        { s: { r: 1, c: 0 }, e: { r: 1, c: 5 } },
        { s: { r: 2,c: 0}, e: { r: 2, c:  2}},
        { s: { r: 2,c: 3}, e: { r: 2, c:  5}},
     ]
    ws['!cols'] = [
        { wpx: 50 }, // 第一列宽度为250单位
        { wpx: 200 }, // 第二列宽度为300单位
        { wpx: 100 },  // 第三列宽度为350单位
        { wpx: 50 },
        { wpx: 50 },
        { wpx: 50 },
    ];
    ws["A1"].s = {
        alignment: { horizontal: 'center' },
        font: { blod: true, sz:24 },
    }
    ws["A2"].s = {
        alignment: { horizontal: 'center' },
        font: { blod: true, sz:12 },
    }
    ws["A3"].s = {
        alignment: { horizontal: 'center' },
        font: { blod: true, sz:12 },
    }
    ws["D3"].s = {
        alignment: { horizontal: 'center' },
        font: { blod: true, sz:12 },
    }
    ws["A4"].s = {
        alignment: { horizontal: 'center' },
        font: { blod: true, color: { rgb: 'ffffff' } },
        fill: { fgColor: { rgb: 'ff6a00' } }
    }
    ws["B4"].s = {
        alignment: { horizontal: 'center' },
        font: { blod: true, color: { rgb: 'ffffff' } },
        fill: { fgColor: { rgb: 'ff6a00' } }
    }
    ws["C4"].s = {
        alignment: { horizontal: 'center' },
        font: { blod: true, color: { rgb: 'ffffff' } },
        fill: { fgColor: { rgb: 'ff6a00' } }
    }
    ws["D4"].s = {
        alignment: { horizontal: 'center' },
        font: { blod: true, color: { rgb: 'ffffff' } },
        fill: { fgColor: { rgb: 'ff6a00' } }
    }
    ws["E4"].s = {
        alignment: { horizontal: 'center' },
        font: { blod: true, color: { rgb: 'ffffff' } },
        fill: { fgColor: { rgb: 'ff6a00' } }
    }
    ws["F4"].s = {
        alignment: { horizontal: 'center' },
        font: { blod: true, color: { rgb: 'ffffff' } },
        fill: { fgColor: { rgb: 'ff6a00' } }
    }

    // 导出工作簿为 Blob 对象
    const wbout = XLSX.write(wb, { bookType: 'xlsx', type: 'binary' });

    // 创建一个 Blob 对象
    const blob = new Blob([s2ab(wbout)], { type: 'application/octet-stream' });

    // 使用 Blob 对象创建一个链接
    const link = document.createElement('a');
    link.href = URL.createObjectURL(blob);


    link.download = `${name} - ${type} -${dayjs().format('MM-DD')}.xlsx`;


    // 触发下载
    link.click();

    // 清理
    URL.revokeObjectURL(link.href);

    function s2ab(s) {
        const buf = new ArrayBuffer(s.length);
        const view = new Uint8Array(buf);
        for (let i = 0; i !== s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
        return buf;
    }

}

//echart 日期补全
export function setData(dateArr) {

	const getNewDate = (time) => {
		let date = new Date(time).getTime() + 1000 * 60 * 60 * 24;
		let Y = new Date(date).getFullYear() + '-';
		let M = (new Date(date).getMonth() + 1 < 10 ? '0' + (new Date(date).getMonth() + 1) : new Date(date)
			.getMonth() + 1) + '-';
		let D = new Date(date).getDate() < 10 ? '0' + new Date(date).getDate() : new Date(date).getDate();
		return Y + M + D;
	};

	//　新日期数组
	let newDateArr = [];
	newDateArr.push(dateArr[0], dateArr[dateArr.length - 1]);
	let startDate = newDateArr[newDateArr.length - 2].date;
	let endDate = newDateArr[newDateArr.length - 1].date;
	let startStamp = new Date(startDate).getTime();
	let endStamp = new Date(endDate).getTime();
	let diffCounts = Math.abs(startStamp - endStamp) / 1000 / 60 / 60 / 24;
	// 得出倒数第二天和倒数第一天的相差天数，依次递减，直到相差１天，表示是相邻的两天
	for (let index = diffCounts; index > 1; index--) {
		//　根据时间戳得出应该往后加的日期
		getNewDate(newDateArr[newDateArr.length - 2].date);
		//　把新的日期从倒数第二个位置追加到新日期数组
		newDateArr.splice(newDateArr.length - 1, 0, {
			date: getNewDate(newDateArr[newDateArr.length - 2].date),
			count: 0,
		});
	}
	newDateArr.forEach((newItem) => {
		dateArr.forEach((oldItem) => {
			if (newItem.date === oldItem.date) {
				newItem.count = oldItem.count;
			}
		});
	});
	// 　　　 console.log('原来的缺失日期',dateArr);
	//       console.log('补全后的日期',newDateArr);
	return newDateArr

}
